﻿using System;

namespace Unijovem.UI.Extensions
{
    public static class StringExtensions
    {
        public static string[] Split(this String stringToSplit, string split)
        {
            var offset = 0;
            var index = 0;
            var offsets = new int[stringToSplit.Length + 1];

            while (index < stringToSplit.Length)
            {
                var indexOf = stringToSplit.IndexOf(split, index, StringComparison.Ordinal);
                if (indexOf != -1)
                {
                    offsets[offset++] = indexOf;
                    index = (indexOf + split.Length);
                }
                else
                    index = stringToSplit.Length;
            }

            var final = new string[offset + 1];
            if (offset == 0)
                final[0] = stringToSplit;
            else
            {
                offset--;
                final[0] = stringToSplit.Substring(0, offsets[0]);
                for (var i = 0; i < offset; i++)
                    final[i + 1] = stringToSplit.Substring(offsets[i] + split.Length,
                                                           offsets[i + 1] - offsets[i] - split.Length);
                final[offset + 1] = stringToSplit.Substring(offsets[offset] + split.Length);
            }
            return final;
        }
    }
}